import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainJFrame2 extends JFrame implements ActionListener {
    private int a=120;
    private int b=138;
    private int TX=375;
    private int TY=400;
    private int TX2=1325;
    private int TY2=400;

    public MainJFrame2(){
        initJFrame();

        initGamePanel();

        createButton();

        setVisible(true);
    }

    private void initJFrame(){
        setSize(1700,1000);
        setTitle("自律的男生制作的小游戏");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(null);
    }

    private void initGamePanel() {
        JPanel jPanel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.RED);
                g.setColor(Color.RED);
                g.fillRect(TX-a/2,TY-b/2,a,b);
                g.setColor(new Color(220,0,0));
                g.fillRect(TX-a/4,TY-a/4,a/2,a/2);
                g.fillRect(TX-a/8,TY-99,a/4,99);

                g.setColor(Color.BLUE);
                g.fillRect(TX2-a/2,TY2-b/2,a,b);
                g.setColor(new Color(0,0,220));
                g.fillRect(TX2-a/4,TY2-a/4,a/2,a/2);
                g.fillRect(TX2-a/8,TY2-99,a/4,99);

                g.setColor(Color.BLACK);
                g.setFont(new Font("宋体", Font.BOLD, 40));

                // 获取文字尺寸用于居中
                FontMetrics fm = g.getFontMetrics();
                String text1 = "W  向前移动";
                String text2 = "S  向后移动";
                String text3 = "A  向左转向";
                String text4 = "D  向右转向";
                String text = "F  发射炮弹(连发上限：5)";
                g.drawString(text1, 255, 180 );
                g.drawString(text2, 255, 640 );
                g.drawString(text3, 0, 350 );
                g.drawString(text4, 525, 350 );
                g.drawString(text, 100, 800 );
                String text5 = "上  向前移动";
                String text6 = "下  向后移动";
                String text7 = "<— 向左转向";
                String text8 = "—> 向右转向";
                String textt = "M  发射炮弹(连发上限：5)";
                g.drawString(text5, 1200, 180 );
                g.drawString(text6, 1200, 640 );
                g.drawString(text7, 950, 350 );
                g.drawString(text8, 1440, 350 );
                g.drawString(textt, 1100, 800 );

                g.setColor(Color.DARK_GRAY);
                g.setFont(new Font("宋体", Font.BOLD, 40));
                String text9 = "玩家1";
                String text10 = "玩家2";
                g.drawString(text9, 320, 80 );
                g.drawString(text10, 1270, 80 );
            }
        };
        jPanel.setBounds(0, 0, 1700, 1000);
        add(jPanel);
    }

    private void createButton(){
        JButton jButton=new JButton("我已知晓操作");
        jButton.setFont(new Font("微软雅黑", Font.BOLD, 50));
        jButton.setBounds(650,700,400,200);
        jButton.addActionListener(this);
        add(jButton);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String command=e.getActionCommand();

        switch (command){
            case "我已知晓操作":
                this.dispose();
                new GameJFrameT();
        }
    }
}
